package com.ikun.hosscheduling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikun.common.entity.Scheduling;
import com.ikun.hosscheduling.mapper.SchedulingMapper;
import com.ikun.hosscheduling.service.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchedulingServiceImpl extends ServiceImpl<SchedulingMapper, Scheduling> implements SchedulingService {

    @Autowired
    private SchedulingMapper schedulingMapper;


    @Override
    public Scheduling addSurplus(String date, Integer docId, Integer timeId) {
        Scheduling scheduling = schedulingMapper.selectOne(new QueryWrapper<Scheduling>().eq("docker_id", docId).eq("date", date));
        if (scheduling != null) {
            Map<String, Integer> map = new HashMap<>();
            String code = scheduling.getCode();
            String[] splitCode = code.split(", ");
            for (String s : splitCode) {
                s = s.replace("{", "").replace("}", ""); // 去除括号
                String[] entry = s.split("=");
                map.put(entry[0], Integer.valueOf(entry[1]));
            }
            //根据时间段id找到对应的余号
            int surplus = map.get(timeId.toString()) + 1;
            map.put(String.valueOf(timeId), surplus);

            //修改完成后的code
            String updateCode = map.toString();

            scheduling.setCode(updateCode);

            schedulingMapper.updateById(scheduling);
        }
        return scheduling;
    }

    @Override
    public Scheduling reduceSurplus(String date, Integer docId, Integer timeId) {
        Scheduling scheduling = schedulingMapper.selectOne(new QueryWrapper<Scheduling>().eq("docker_id", docId).eq("date", date));
        if (scheduling != null) {
            Map<String, Integer> map = new HashMap<>();
            String code = scheduling.getCode();
            String[] splitCode = code.split(", ");

            for (String s : splitCode) {
                s = s.replace("{", "").replace("}", ""); // 去除括号
                String[] entry = s.split("=");
                map.put(entry[0], Integer.valueOf(entry[1]));
            }
            //根据时间段id找到对应的余号
            int surplus = map.get(timeId.toString()) - 1;
            map.put(String.valueOf(timeId), surplus);

            //修改完成后的code
            String updateCode = map.toString();

            scheduling.setCode(updateCode);

            schedulingMapper.updateById(scheduling);
        }
        return scheduling;
    }

    @Override
    public List<Scheduling> getScheduleByDate(LocalDate data) {
        List<Scheduling> schedulingList = schedulingMapper.findByDate(data);
        return schedulingList;
    }

    @Override
    public List<Scheduling> getScheduleByDocker(Long dockerId, LocalDate date, Long positionId) {
        List<Scheduling> schedulingDocker = schedulingMapper.findByDocker(dockerId, date, positionId);
        return schedulingDocker;
    }
}
